package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.wemedia.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/v1/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
     * 素材列表查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto dto) {
        //参数校验
        //将page size 进行范围判断 设置合理值
        dto.checkParam();
        //PageResponseResult pageResponseResult =  newsService.findNews(dto);
        //第二种作法就是为了不让和前段产生依赖
        //{code msg data:{page size list total}}
        //{code msg data : [] page size total}
        // PageResponseVO result = newsService.findNews(dto);
        //return ResponseResult.okResult(result);
        PageResponseResult pageResponseResult = newsService.findNews(dto);
        return pageResponseResult;

    }


    @PostMapping("/submit")
    //@Valid自动效验
    public ResponseResult submit(@Valid @RequestBody WmNewsDto dto) {
        //参数校验 用注解方式完成
        newsService.submit(dto);

        return ResponseResult.okResult(null);
    }
}
